<template>
  <a-modal
    :width='800'
    :visible='visible'
    :confirm-loading='loading'
    :forceRender="true"
    :title="isUpdate ? '修改' : '新建'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible='updateVisible'
    @ok='save'
  >
    <a-form
      ref='form'
      :model='form'
      :rules='rules'
      :label-col='{ md: { span: 4 }, sm: { span: 24 } }'
      :wrapper-col='{ md: { span: 20 }, sm: { span: 24 } }'
    >
      <a-form-item label='主机名称:' name='hostMachineName'>
        <a-input v-model:value='form.hostMachineName' placeholder='请输入主机名称' allow-clear autocomplete='off' />
      </a-form-item>
      <a-form-item label='主机ip:' name='hostMachineIp'>
        <a-input v-model:value='form.hostMachineIp' placeholder='请输入主机ip' allow-clear autocomplete='off' />
      </a-form-item>
      <a-form-item label='操作系统:' name='operatingSystem'>
        <a-input v-model:value='form.operatingSystem' placeholder='请输入操作系统' allow-clear autocomplete='off' />
      </a-form-item>
      <a-form-item label='SSH端口:' name='sshPort'>
        <a-input v-model:value='form.sshPort' placeholder='请输入SSH端口' allow-clear autocomplete='off' />
      </a-form-item>
      <a-form-item label='认证方式：1-密码，2-秘钥:' name='authenticationType'>
        <a-input-number v-model:value='form.authenticationType' placeholder='请输入认证方式：1-密码，2-秘钥' allow-clear autocomplete='off' style="width: 100%" />
      </a-form-item>
      <a-form-item label='主机用户名:' name='hostMachineUserName'>
        <a-input v-model:value='form.hostMachineUserName' placeholder='请输入主机用户名' allow-clear autocomplete='off' />
      </a-form-item>
      <a-form-item label='主机密码:' name='hostMachinePassword'>
        <a-input v-model:value='form.hostMachinePassword' placeholder='请输入主机密码' allow-clear autocomplete='off' />
      </a-form-item>
      <a-form-item label='秘钥密码:' name='hostSecretKeyCipher'>
        <a-input v-model:value='form.hostSecretKeyCipher' placeholder='请输入秘钥密码' allow-clear autocomplete='off' />
      </a-form-item>
      <a-form-item label='主机状态：1-在线，2-离线:' name='hostMachineState'>
        <a-input-number v-model:value='form.hostMachineState' placeholder='请输入主机状态：1-在线，2-离线' allow-clear autocomplete='off' style="width: 100%" />
      </a-form-item>
      <a-form-item label='是否是公司级主机：Y-是，N-否:' name='companyFlag'>
        <a-input v-model:value='form.companyFlag' placeholder='请输入是否是公司级主机：Y-是，N-否' allow-clear autocomplete='off' />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script>
import { message } from 'ant-design-vue';
import { DevopsDoHostMachineApi } from '/@/api/system/operation/DevopsDoHostMachineApi';

export default {
  name: 'DevopsDoHostMachineEdit',
  components: {
  },
  emits: ['done', 'update:visible'],
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({}, this.data),
      // 表单验证规则
      rules: {
        hostMachineName: [{ required: true, message: '请输入主机名称', type: 'string', trigger: 'blur' }],
        hostMachineIp: [{ required: true, message: '请输入主机ip', type: 'string', trigger: 'blur' }],
        operatingSystem: [{ required: true, message: '请输入操作系统', type: 'string', trigger: 'blur' }],
        sshPort: [{ required: true, message: '请输入SSH端口', type: 'string', trigger: 'blur' }],
        authenticationType: [{ required: true, message: '请输入认证方式：1-密码，2-秘钥', type: 'number', trigger: 'blur' }],
        hostMachineUserName: [{ required: true, message: '请输入主机用户名', type: 'string', trigger: 'blur' }],
        hostMachinePassword: [{ required: true, message: '请输入主机密码', type: 'string', trigger: 'blur' }],
        hostSecretKeyCipher: [{ required: true, message: '请输入秘钥密码', type: 'string', trigger: 'blur' }],
        hostMachineState: [{ required: true, message: '请输入主机状态：1-在线，2-离线', type: 'number', trigger: 'blur' }],
        companyFlag: [{ required: true, message: '请输入是否是公司级主机：Y-是，N-否', type: 'string', trigger: 'blur' }]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    visible() {
      if (this.visible) {
        if (this.data) {
          this.form = Object.assign({}, this.data);
          this.isUpdate = true;
        } else {
          this.form = {};
          this.isUpdate = false;
        }
        if (this.$refs.form) {
          this.$refs.form.clearValidate();
        }
      }
    }
  },
  methods: {
    /**
     * 保存和编辑
     *
     * @author sakanayy
     * @date 2023/06/29 15:15
     */
    async save() {
      // 校验表单
      await this.$refs.form.validate();

      // 修改加载框为正在加载
      this.loading = true;

      let result = null;

      // 执行编辑或修改
      if (this.isUpdate) {
        result = DevopsDoHostMachineApi.edit(this.form);
      } else {
        result = DevopsDoHostMachineApi.add(this.form);
      }
      result
        .then(result => {
          // 移除加载框
          this.loading = false;

          // 提示添加成功
          message.success(result.message);

          // 如果是新增，则form表单置空
          if (!this.isUpdate) {
            this.form = {};
          }

          // 关闭弹框，通过控制visible的值，传递给父组件
          this.updateVisible(false);

          // 触发父组件done事件
          this.$emit('done');
        })
        .catch(() => {
          this.loading = false;
        });
    },

    /**
     * 更新编辑界面的弹框是否显示
     *
     * @param value true-显示，false-隐藏
     * @author sakanayy
     * @date 2023/06/29 15:15
     */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
};
</script>
